package com.leetcode.partition3;

/**
 * @author `RKC`
 * @date 2021/10/11 12:19
 */
public class LC273整数转换英文表示 {

    private static final String[] singles = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
    private static final String[] teens = {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
    private static final String[] tens = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
    private static final String[] thousands = {"", "Thousand", "Million", "Billion"};

    public static void main(String[] args) {
        System.out.println(numberToWords(567219283));
    }

    public static String numberToWords(int num) {
        //非负整数最大值2^31 -1，最多只有10位数，每3位为一组
        //小于20的可以直接用英文表示
        //大于等于20小于100的数先将十位转成英文表示，然后对各位递归转成英文
        //大于等于100的将百位转成英文，然后对其余部分（十位和个位）递归转换成英文
        if (num == 0) return "Zero";
        StringBuilder sb = new StringBuilder();
        //i指向单位
        for (int i = 3, unit = 1000000000; i >= 0; i--, unit /= 1000) {
            //三位为一组的将num分割出来
            int cur = num / unit;
            System.out.println(cur);
            if (cur != 0) {
                num -= cur * unit;
                StringBuilder curSb = new StringBuilder();
                recursion(curSb, cur);
                curSb.append(thousands[i]).append(" ");
                sb.append(curSb);
            }
        }
        return sb.toString().trim();
    }

    private static void recursion(StringBuilder sb, int num) {
        if (num == 0) return;
        if (num < 10) sb.append(singles[num]).append(" ");
        else if (num < 20) sb.append(teens[num - 10]).append(" ");
        else if (num < 100) {
            sb.append(tens[num / 10]).append(" ");
            recursion(sb, num % 10);
        } else {
            sb.append(singles[num / 100]).append(" Hundred ");
            recursion(sb, num % 100);
        }
    }
}
